package kr.co.jnu.UND;

import java.io.File;
import java.io.FileOutputStream;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class UNDActivity extends Activity {
	static final String fileName = "bg.jpg";
	Bitmap bg;
	File bgPath;

	Button btn[] = new Button[4];
	TextView text_mode;
	int user_leng = 100;
	int gameMode = MainActivity.MODE_DEFAULT;
	String strUser;

	private SharedPreferences mPrefs;
	private SharedPreferences.Editor ed;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.main);

		mPrefs = getSharedPreferences("preference", Activity.MODE_PRIVATE);
		ed = mPrefs.edit();
		gameMode = mPrefs.getInt("GAME_MODE", MainActivity.MODE_DEFAULT);

		btn[0] = (Button) findViewById(R.id.button1);
		btn[1] = (Button) findViewById(R.id.button2);
		btn[2] = (Button) findViewById(R.id.button3);
		btn[3] = (Button) findViewById(R.id.button4);
		for (int i = 0; i < 4; i++) {
			btn[i].setOnClickListener(onBtnClick);
			btn[i].setTextSize(20);
		}

		text_mode = (TextView) findViewById(R.id.modeInfo);

		user_leng = Integer.parseInt(PrefActivity.getLength(this));
		if (user_leng < 2) {
			Toast.makeText(getApplicationContext(),
					getString(R.string.user_min_length), Toast.LENGTH_SHORT)
					.show();
		} else if (user_leng > 1000) {
			Toast.makeText(getApplicationContext(),
					getString(R.string.user_max_length), Toast.LENGTH_SHORT)
					.show();
		}
		strUser = getString(R.string.user);

		btn[3].setText(user_leng + "\n(" + strUser + ")");
		setMode();

		String root = Environment.getExternalStorageDirectory().toString();
		bgPath = new File(root + "/und");
		bgPath.mkdirs();
		File file = new File(bgPath, fileName);

		if (!file.exists()) {			
			bg = BitmapFactory.decodeResource(getResources(), R.drawable.bg01);
			SaveBitmap(bg);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		SubMenu sub = menu.addSubMenu(R.string.menu_item_1);
		sub.add(0, 11, 0, R.string.menu_item_1_sub_1);
		sub.add(0, 12, 0, R.string.menu_item_1_sub_2);
		menu.add(0, 20, 0, getString(R.string.menu_item_2));
		menu.add(0, 30, 0, getString(R.string.menu_item_3));
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case 11:
			gameMode = MainActivity.MODE_DEFAULT;
			ed.putInt("GAME_MODE", gameMode);
			ed.commit();
			setMode();
			break;
		case 12:
			gameMode = MainActivity.MODE_SUDDEN_DEATH;
			ed.putInt("GAME_MODE", gameMode);
			ed.commit();
			setMode();
			break;
		case 20:
			Intent intent = new Intent(UNDActivity.this, PrefActivity.class);
			startActivityForResult(intent, 1);
			break;
		case 30:
			Intent bgintent = new Intent(UNDActivity.this, SetBgActivity.class);
			startActivity(bgintent);
		}
		return true;
	}

	View.OnClickListener onBtnClick = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			int length = 50;
			switch (v.getId()) {
			case R.id.button1:
				length = Integer.parseInt(getString(R.string.main_btn_1));
				break;
			case R.id.button2:
				length = Integer.parseInt(getString(R.string.main_btn_2));
				break;
			case R.id.button3:
				length = Integer.parseInt(getString(R.string.main_btn_3));
				break;
			case R.id.button4:
				length = user_leng;
				break;
			}
			if (length < 2) {
				Toast.makeText(getApplicationContext(),
						getString(R.string.user_min_length), Toast.LENGTH_SHORT)
						.show();
			} else if (length > 1000) {
				Toast.makeText(getApplicationContext(),
						getString(R.string.user_max_length), Toast.LENGTH_SHORT)
						.show();
			} else {
				Intent intent = new Intent(UNDActivity.this, MainActivity.class);
				intent.putExtra("LENGTH", length);
				intent.putExtra("MODE", gameMode);
				startActivity(intent);
				finish();
			}
		}
	};

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == 1) {
			user_leng = Integer.parseInt(PrefActivity.getLength(this));
			btn[3].setText(user_leng + "\n(" + strUser + ")");
		}
	}

	private void setMode() {
		if (gameMode == MainActivity.MODE_DEFAULT) {
			text_mode.setText(getText(R.string.mode_default));
		} else if (gameMode == MainActivity.MODE_SUDDEN_DEATH) {
			text_mode.setText(getText(R.string.mode_sudden_death));
		}
	}

	private void SaveBitmap(Bitmap bmp) {
		File file = new File(bgPath, fileName);
		if (file.exists())
			file.delete();
		try {
			FileOutputStream out = new FileOutputStream(file);
			bmp.compress(Bitmap.CompressFormat.JPEG, 90, out);
			out.flush();
			out.close();
		} catch (Exception e) {
			Toast.makeText(getApplicationContext(), "err", 0).show();
			e.printStackTrace();
		}
	}
}
